/*
 * Title: Delete from Disk Extension
 * Author: Farhan Ahmad <farhan@thebitguru.com>
 * Website: http://www.thebitguru.com
 * 
 * This extensions adds a menu entry "Delete from Disk" to the playlist context
 * menu to delete the file from the file system in addition to the playlist.
 */

// Make a namespace.
var DeleteFromDisk = {};

DeleteFromDisk.Controller = {
	init: function() {
    this.initialized = true;
	},

	// Called when the popup menu is shown.  This is used to hide and show the menu
	// item based on whether any items are selected.
	popupMenuShowing: function() {
		var numItems = window.mediaPage._playlist.getListView().selection.count;
		document.getElementById('delete-from-disk-delete-menuitem').hidden = numItems == 0;
		return true;
	},

	// Called when the menu item is clicked.  This confirms with the user and then the
	// actual deletion.
	menuClicked: function() {
		if (!window.SBProperties) Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
		
		var mediaListView = window.mediaPage._playlist.getListView();
		var list = mediaListView.selection.selectedIndexedMediaItems;
		var numItems = mediaListView.selection.count;
		
		var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
			.getService(Components.interfaces.nsIPromptService);

		var checkValue = {};  // Because it is required.
		var flags = promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_0 +
			promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_1;
		
		var title = "Are you sure that you want to delete ";
		title += (numItems == 1 ? "this file" : "these files");
		title += ". This CANNOT be undone!";
		var button = promptService.confirmEx(window, "Delete?", title, flags,
				"Yes, delete", "Cancel", null, null, checkValue);
		if (button != 0) {
			return;
		}
		
		var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
		var fileHandler = ioService.getProtocolHandler("file").QueryInterface(Ci.nsIFileProtocolHandler);
		var libraryManager = Components.classes["@songbirdnest.com/Songbird/library/Manager;1"]
			.getService(Components.interfaces.sbILibraryManager); // Get the user’s main library
		var mainLib = libraryManager.mainLibrary; 

		try {
			while (list.hasMoreElements()) {
				var mediaItem = list.getNext().mediaItem;
				var mediaItemFile = fileHandler.getFileFromURLSpec(
						mediaItem.getProperty(SBProperties.contentURL)); // nsIFile

				// Delete from file system.  parameter = Don't do recursive delete.
				mediaItemFile.remove(false);
				
				// Deleting from the main library will remove it from all playlists as well so we
				// _do not_ need to remove it from at least the selected playlist.
				mainLib.remove(mediaItem);

				// Delete for playlist, left for reference, see comment accompanying mainLib.remove.
				// mediaListView.mediaList.remove(mediaItem);
			}
		}
		catch (err) {
			if (err.name && (err.name == "NS_ERROR_FILE_NOT_FOUND")) {
				promptService.alert(window, "Error while deleting", "This file no longer exists at the original location. Aborting.");
			} else {
				promptService.alert(window, "Error while deleting", "Could not delete file, aborting.\n\n" + err);
			}
		}
	}
}
	
DeleteFromDisk.Controller.init();  // Initialize the controller.
